package main

import (
	"context"
	"fmt"
	"github.com/golang/protobuf/ptypes/empty"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"kuangstudy-mall/srvs/goods-srv/proto"
)

var goodsClient proto.GoodsClient

func InitClient() {
	conn, err := grpc.Dial("127.0.0.1:9100", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	goodsClient = proto.NewGoodsClient(conn)
}

func BannerList() {
	request := proto.BannerInfoRequest{Stype: 2}
	list, err := goodsClient.FindBannerList(context.Background(), &request)
	if err != nil {
		panic(err)
	}
	fmt.Println(list)
}

func categoryList() {
	request := proto.CategoryInfoRequest{IsIndex: 1}
	list, err := goodsClient.FindCategoryListToIndex(context.Background(), &request)
	if err != nil {
		panic(err)
	}
	fmt.Println(list)
}

func categoryListAll() {
	list, err := goodsClient.FindCategoryList(context.Background(), &empty.Empty{})
	if err != nil {
		panic(err)
	}
	fmt.Println(list)
}

func goodList() {
	request := proto.GoodsFilterRequest{PageNo: 1, PageSize: 10, Keyword: "飞", IsNew: 1, IsHot: -1, CategoryId: 1}
	list, err := goodsClient.FindGoodsList(context.Background(), &request)
	if err != nil {
		panic(err)
	}
	fmt.Println(list)
}

func goodById() {
	request := proto.GoodsInfoRequest{Id: 2}
	goods, err := goodsClient.GetGoodsDetail(context.Background(), &request)
	if err != nil {
		panic(err)
	}
	fmt.Println(goods)
}

func batchGoodsList() {
	goodsIds := []uint64{1, 2, 3, 4}
	request := proto.BatchGoodsInfo{Ids: goodsIds}
	goods, err := goodsClient.BatchGetGoodsList(context.Background(), &request)
	if err != nil {
		panic(err)
	}
	fmt.Println(goods)
}

func main() {
	InitClient()
	// 测试查询banner
	//BannerList()
	// 测试分类查询
	//categoryList()
	// 查询所有分类
	//categoryListAll()
	// 商品查询测试
	//goodList()
	// 根据id查询商品信息
	//goodById()
	// 批量查询商品信息
	batchGoodsList()
}
